﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculatorFlexibe.Implementations;
using CalculatorFlexibe.Interfaces;

namespace CalculatorFlexibe
{
    class CalculatorFacade
    {
        private ExpressionPreParse preParse;
        private ExpressionIsValid isValid;
        private Calculator calculator;
        public CalculatorFacade()
            : this(new ExpressionPreParse(),
                   new ExpressionIsValid(new StaticUploader(),
                                         new NullPOParanthesis()),
                   new CorrectValueInsert(), 
                   new GetLROperands(), 
                   new NullPOParanthesis(),
                   new StaticUploader())
        {
        }
        public CalculatorFacade(ExpressionPreParse preParse, 
                                ExpressionIsValid isValid, 
                                IChanger changer,
                                IGetOperands getter,
                                INullPriorityOperation nop,
                                IUploader uploader)
        {
            this.preParse = preParse;
            this.isValid = isValid;
            this.calculator = new Calculator(changer, getter, nop, uploader);
        }
        public double Execute(string expr)
        {
            expr = preParse.CallAllMethods(expr);
            if (isValid.IsValid(expr))
                return Convert.ToDouble(calculator.Execute(expr));
            else throw new Exception("Формат строки некорректен");
        }
    }
}
